go to previous page   go to home page   go to next page

Answer:

No. Something is wrong with the last line of the program.


Two things to get Right

In translating the math-like definition into Java:

Usually in the Java translation, the base case is detected in the condition of an if-statement. Sometimes there are several base cases. Be sure to test for each one.

In the previous question, the mistaken translation into Java failed to divide the problem into smaller parts.


QUESTION 11:

Here is the math-like definition, again:

Triangle( 1 ) = 1
Triangle( N ) = N + Triangle( N-1 )

Is the following a correct translation of this definition?

int Triangle( int N )
{
  if ( N != 1 )
    return 1;
  else
    return N + Triangle( N-1 );
}